/**
 * 
 */
package co.fitstart.entity.order;

import java.beans.Transient;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicUpdate;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.product.Product;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.investigation.FitInvestigation;
import co.fitstart.entity.user.investigation.HealthInvestigation;
import co.fitstart.entity.user.investigation.ParticipateInvestigation;
import co.fitstart.entity.user.investigation.ProfileInvestigation;

/**
 * @author mohanlan
 *
 */
@Entity
@Table(name = "order_investigate_record")
@DynamicUpdate
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class OrderInvestigateRecord {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 冗余字段 便于从学生角度查询 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JSONField(serialize = false)
    private Student student;
    
    /** 冗余字段 便于从订单角度查询 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JSONField(serialize = false)
    private Order order;
    
//    /** 冗余字段 便于从课程角度查询 */
//    @ManyToOne(fetch = FetchType.LAZY, optional = false)
//    @JSONField(serialize = false)
//    private CourseRound courseRound;
    
    /** 是否需要 profile investigation */
    private boolean profile;
    
    /** 是否需要 fit investigation */
    private boolean fit;
    
    /** 是否需要 health investigation */
    private boolean health;
    
    /** 是否需要 participate investigation */
    private boolean participate;
    
    /** 个人信息调查 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private ProfileInvestigation profileInvestigation;
    
    /** 身体信息调查 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private FitInvestigation fitInvestigation;
    
    /** 健康调查 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private HealthInvestigation healthInvestigation;
    
    /** 参与度调查 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private ParticipateInvestigation participateInvestigation;
    
    /** 第三方问卷调查地址 */
    private String thirdPart;
    
    /** 是否完成问卷调查 */
    private boolean finish;
    
    /** 用户备注 */
    private String userRemarks;
    
    /** 二维码上传状态 */
    private boolean qrcodeStatus;
    
    /** 二维码上传备注 */
    private String qrcodeRemarks;
    
    
    /**
     * The default constructor
     */
    public OrderInvestigateRecord() {
    }
    
    /**
     * 
     * @param order
     */
    public OrderInvestigateRecord(Order order) {
        this.student = order.getStudent();
        this.order = order;
        this.finish = false;
        
        if (order.getOrderType().isOffline()) {
            this.initializeForOffline();
        } else {
            this.initializeForOnline();
        }
        
    }
    
    /**
     * @return
     */
    protected OrderInvestigateRecord initializeForOffline() {
        this.fit = true;
        this.health = false;
        this.participate = false;
        this.profile = false;
        return this;
    }

    /**
     * 
     * @return
     */
    protected OrderInvestigateRecord initializeForOnline() {
        
        Product product = this.order.getProduct();
        this.profile = true;
        this.fit = true;
        
        if(product.isThirdPartyProduct() && StringUtils.equalsIgnoreCase(product.getThirdParty(), "duonang")) {
            this.health = false;
            this.participate = false;
            this.thirdPart = "http://form.mikecrm.com/WsyaS7?from=singlemessage&isappinstalled=0";
        }else {
            this.health = true;
            this.participate = true;
            this.thirdPart = null;
        }
        
        return this;
    }

    /**
     * 
     * @return
     */
    public boolean needProfileInvestigation() {
        return this.profile && (this.profileInvestigation == null || this.profileInvestigation.getId() == null); 
    }

    /**
     * 
     * @return
     */
    public boolean needFitInvestigation() {
        return this.fit && (this.fitInvestigation == null || this.fitInvestigation.getId() == null);
    }

    /**
     * 
     * @return
     */
    public boolean needHealthInvestigation() {
        return this.health && (this.healthInvestigation == null || this.healthInvestigation.getId() == null);
    }

    /**
     * 
     * @return
     */
    public boolean needParticipateInvestigation() {
        return this.participate && (this.participateInvestigation == null || this.participateInvestigation.getId() == null);
    }

    /**
     * @return
     */
    public boolean gotoThirdPart() {
        return this.finish == true && StringUtils.isNoneEmpty(this.thirdPart);
    }

    /**
     * 
     */
    protected void doFinish() {
        this.finish = !this.needProfileInvestigation() && !this.needFitInvestigation() && !this.needFitInvestigation() && !this.needParticipateInvestigation();
    }

    /**
     * 
     * @param profileInvestigation
     * @return
     */
    public OrderInvestigateRecord profileInvestigation(ProfileInvestigation profileInvestigation) {
        this.profileInvestigation = profileInvestigation;
        this.userRemarks = profileInvestigation.getRemarks();
        this.doFinish();
        return this;
    }

    /**
     * 
     * @param fitInvestigation
     * @return
     */
    public OrderInvestigateRecord fitInvestigation(FitInvestigation fitInvestigation) {
        this.fitInvestigation = fitInvestigation;
        this.doFinish();
        return this;
    }

    /**
     * 
     * @param healthInvestigation
     * @return
     */
    public OrderInvestigateRecord healthInvestigation(HealthInvestigation healthInvestigation) {
        this.healthInvestigation = healthInvestigation;
        this.doFinish();
        return this;
    }

    /**
     * 
     * @param participateInvestigation
     * @return
     */
    public OrderInvestigateRecord participateInvestigation(ParticipateInvestigation participateInvestigation) {
        this.participateInvestigation = participateInvestigation;
        this.doFinish();
        return this;
    }
    
    /**
     * 
     * @return
     */
    public OrderInvestigateRecord saveQrcodeSuccess() {
        this.qrcodeStatus = true;
        this.qrcodeRemarks = null;
        return this;
    }
    
    /**
     * 
     * @param qrcodeRemarks
     * @return
     */
    public OrderInvestigateRecord saveQrcodeFailure(String qrcodeRemarks) {
        this.qrcodeStatus = false;
        this.qrcodeRemarks = qrcodeRemarks;
        return this;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isProfileInvestigation() {
        if(this.profileInvestigation == null || this.profileInvestigation.getId() == null) {
            return false;
        }
        return true;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isFitInvestigation() {
        if(this.fitInvestigation == null || this.fitInvestigation.getId() == null) {
            return false;
        }
        return true;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isHealthInvestigation() {
        if(this.healthInvestigation == null || this.healthInvestigation.getId() == null) {
            return false;
        }
        return true;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isParticipateInvestigation() {
        if(this.participateInvestigation == null || this.participateInvestigation.getId() == null) {
            return false;
        }
        return true;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String getExpectation() {
        if(this.participate && this.participateInvestigation != null && this.participateInvestigation.getId() != null) {
            return this.participateInvestigation.getInvestigationExpectation();
        }
        return "";
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String getReason() {
        if(this.participate && this.participateInvestigation != null && this.participateInvestigation.getId() != null) {
            return this.participateInvestigation.getInvestigationReason();
        }
        return "";
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String getSource() {
        if(this.participate && this.participateInvestigation != null && this.participateInvestigation.getId() != null) {
            return this.participateInvestigation.getInvestigationSource();
        }
        return "";
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the student
     */
    public Student getStudent() {
        return student;
    }

    /**
     * @return the order
     */
    public Order getOrder() {
        return order;
    }

    /**
     * @return the profile
     */
    public boolean isProfile() {
        return profile;
    }

    /**
     * @return the fit
     */
    public boolean isFit() {
        return fit;
    }

    /**
     * @return the health
     */
    public boolean isHealth() {
        return health;
    }

    /**
     * @return the participate
     */
    public boolean isParticipate() {
        return participate;
    }

    /**
     * @return the profileInvestigation
     */
    public ProfileInvestigation getProfileInvestigation() {
        return profileInvestigation;
    }

    /**
     * @return the fitInvestigation
     */
    public FitInvestigation getFitInvestigation() {
        return fitInvestigation;
    }

    /**
     * @return the healthInvestigation
     */
    public HealthInvestigation getHealthInvestigation() {
        return healthInvestigation;
    }

    /**
     * @return the participateInvestigation
     */
    public ParticipateInvestigation getParticipateInvestigation() {
        return participateInvestigation;
    }

    /**
     * @return the thirdPart
     */
    public String getThirdPart() {
        return thirdPart;
    }

    /**
     * @return the finish
     */
    public boolean isFinish() {
        return finish;
    }

    /**
     * @return the userRemarks
     */
    public String getUserRemarks() {
        return userRemarks;
    }

    /**
     * @return the qrcodeStatus
     */
    public boolean isQrcodeStatus() {
        return qrcodeStatus;
    }

    /**
     * @return the qrcodeRemarks
     */
    public String getQrcodeRemarks() {
        return qrcodeRemarks;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param student the student to set
     */
    public void setStudent(Student student) {
        this.student = student;
    }

    /**
     * @param order the order to set
     */
    public void setOrder(Order order) {
        this.order = order;
    }

    /**
     * @param profile the profile to set
     */
    public void setProfile(boolean profile) {
        this.profile = profile;
    }

    /**
     * @param fit the fit to set
     */
    public void setFit(boolean fit) {
        this.fit = fit;
    }

    /**
     * @param health the health to set
     */
    public void setHealth(boolean health) {
        this.health = health;
    }

    /**
     * @param participate the participate to set
     */
    public void setParticipate(boolean participate) {
        this.participate = participate;
    }

    /**
     * @param profileInvestigation the profileInvestigation to set
     */
    public void setProfileInvestigation(ProfileInvestigation profileInvestigation) {
        this.profileInvestigation = profileInvestigation;
    }

    /**
     * @param fitInvestigation the fitInvestigation to set
     */
    public void setFitInvestigation(FitInvestigation fitInvestigation) {
        this.fitInvestigation = fitInvestigation;
    }

    /**
     * @param healthInvestigation the healthInvestigation to set
     */
    public void setHealthInvestigation(HealthInvestigation healthInvestigation) {
        this.healthInvestigation = healthInvestigation;
    }

    /**
     * @param participateInvestigation the participateInvestigation to set
     */
    public void setParticipateInvestigation(ParticipateInvestigation participateInvestigation) {
        this.participateInvestigation = participateInvestigation;
    }

    /**
     * @param thirdPart the thirdPart to set
     */
    public void setThirdPart(String thirdPart) {
        this.thirdPart = thirdPart;
    }

    /**
     * @param finish the finish to set
     */
    public void setFinish(boolean finish) {
        this.finish = finish;
    }

    /**
     * @param userRemarks the userRemarks to set
     */
    public void setUserRemarks(String userRemarks) {
        this.userRemarks = userRemarks;
    }

    /**
     * @param qrcodeStatus the qrcodeStatus to set
     */
    public void setQrcodeStatus(boolean qrcodeStatus) {
        this.qrcodeStatus = qrcodeStatus;
    }

    /**
     * @param qrcodeRemarks the qrcodeRemarks to set
     */
    public void setQrcodeRemarks(String qrcodeRemarks) {
        this.qrcodeRemarks = qrcodeRemarks;
    }

//    /**
//     * @return the courseRound
//     */
//    public CourseRound getCourseRound() {
//        return courseRound;
//    }


//    /**
//     * @param courseRound the courseRound to set
//     */
//    public void setCourseRound(CourseRound courseRound) {
//        this.courseRound = courseRound;
//    }

}
